package com.golddaymicromall.activity.recharge;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.PayDemo.PayDemoActivity;
import com.golddaymicromall.activity.Constants;
import com.golddaymicromall.activity.PayActivity;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class ChoicePayActivity extends BaseActivity implements View.OnClickListener {
    private Button mConfirmPay;
    private String Allprice;
    private String morderName;
    private String Flag;
    private String Ids;
    private String ProductName;
    private ImageView mAliPay;
    private ImageView myibaopay;
    private int payFlag = 3; //1是微信支付，2是支付宝,3易宝支付
    private TextView mProductOrderId;
    private TextView mProductPrice;
    private LinearLayout mPayment_back;
    private PayReq req;
    final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
    private StringBuffer sb;
    private String customerid;
    private String Customerno;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice_pay);
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Customerno = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        req = new PayReq();
        sb = new StringBuffer();
        msgApi.registerApp(Constants.APP_ID);
        initView();
        Allprice = getIntent().getStringExtra("mAllPrice");
        morderName = getIntent().getStringExtra("morderName");
        if (!"".equals(morderName) || null != morderName) {
            mProductOrderId.setText(morderName);
        }
        mProductPrice.setText("￥" + Allprice);
    }

    private void initView() {
        mConfirmPay = (Button) findViewById(R.id.confirmpay);
        mAliPay = (ImageView) findViewById(R.id.alipay);
        myibaopay = (ImageView) findViewById(R.id.yibaopay);
        mProductOrderId = (TextView) findViewById(R.id.productOrderId);
        mProductPrice = (TextView) findViewById(R.id.productPrice);
        mPayment_back = (LinearLayout) findViewById(R.id.payment_back);
        mPayment_back.setOnClickListener(this);
        mConfirmPay.setOnClickListener(this);
        myibaopay.setOnClickListener(this);
        mAliPay.setOnClickListener(this);
        myibaopay.setImageResource(R.drawable.pay);
        mAliPay.setImageResource(R.drawable.pay_none);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.payment_back:
                finish();
                break;
            case R.id.yibaopay:
                payFlag = 3;
                clearChecked();
                myibaopay.setImageResource(R.drawable.pay);
                mAliPay.setImageResource(R.drawable.pay_none);
                break;

            case R.id.alipay:
                payFlag = 2;
                clearChecked();
                mAliPay.setImageResource(R.drawable.pay);
                myibaopay.setImageResource(R.drawable.pay_none);
                break;
            case R.id.confirmpay:
                if (payFlag == 1) {
                    Intent intent2wx = new Intent(this, PayActivity.class);
                    intent2wx.putExtra("mAllPrice", Allprice);
                    intent2wx.putExtra("mProductName", ProductName);
                    startActivity(intent2wx);
                } else if (payFlag == 2) {
                    attemptzhifubao();
                } else if (payFlag == 3) {
                    attemptyibao();
                }
                break;
            default:
                break;
        }
    }

    public void attemptyibao() {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.onlineremittancetwo.add");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("billno", morderName);
        form.put("customerid", customerid);
        form.put("amount", Allprice);
        form.put("paytype", "易宝");
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            if (Integer.parseInt(result) == 1) {
                                Toast.makeText(ChoicePayActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ChoicePayActivity.this, ZaiXianhuiKuanWebActivity.class);
                                intent.putExtra("mAllPrice", Allprice);
                                Log.e("Allprice", Allprice);
                                intent.putExtra("morderName", mProductOrderId.getText());
                                startActivity(intent);
                                finish();
                                MyApplication.getInstance().attemptRiZhi(Customerno, "易宝支付"+Allprice);
                            } else {
                                Toast.makeText(ChoicePayActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                },throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    public void attemptzhifubao() {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.onlineremittancetwo.add");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("billno", morderName);
        form.put("customerid", customerid);
        form.put("amount", Allprice);
        form.put("paytype", "支付宝");
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(),RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            if (Integer.parseInt(result) == 1) {
                                Toast.makeText(ChoicePayActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ChoicePayActivity.this, PayDemoActivity.class);
                                intent.putExtra("mAllPrice", Allprice);
                                intent.putExtra("morderName", mProductOrderId.getText());
                                startActivity(intent);
                                finish();
                                MyApplication.getInstance().attemptRiZhi(Customerno, "支付宝支付"+Allprice);
                            } else {
                                Toast.makeText(ChoicePayActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                },throwable -> {
                    showError();
                    throwable.printStackTrace();
                });
    }

    public void clearChecked() {
        mAliPay.setImageResource(R.drawable.pay_none);
        myibaopay.setImageResource(R.drawable.pay_none);
    }
}
